<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/5/15
 * Time: 17:37
 */

namespace common\services;

use Yii;
use common\models\FavoriteMember;
use yii\data\Pagination;
use common\library\comFun;

class FavoriteMemberSve extends BaseSve
{

    /*
     * 获取列表
     */
    public function getList($where = [], string $orderBy = '', bool $isPaging = false):array
    {
        $Model = FavoriteMember::find();
        $Model->joinWith('member', false, 'LEFT JOIN');
        if (!empty($where)) {
            $Model->where($where);
        }
        //要返回的数据
        $returnData = [];
        //是否分页
        if ($isPaging) {
            //是
            //查询总数
            $count = $Model->count();
            $pagination = new Pagination(['totalCount' => $count]);
            $request = Yii::$app->request;
            $pageSize = $request->get('page_size', 20);
            if (!comFun::isPositiveInt($pageSize)) {
                //如果分页大小不符合规范，默认20
                $pageSize = 20;
            }
            $pagination->pageSize = $pageSize;

            $page = $request->get('page', 1);

            if (!comFun::isPositiveInt($page)) {
                //如果页数不符合规范，默认1
                $page = 1;
            }

            $pagination->page = --$page;
            $list = $Model
                ->addSelect(['`favorite_member`.*,`member`.member_name,`member`.nickname,`member`.avatar,`member`.sex,`member`.introduction'])
                ->orderBy($orderBy)
                ->offset($pagination->offset)
                ->limit($pagination->limit)->asArray()->all();
            $returnData['list'] = $list;
            $returnData['pageArr'] = [
                'pageSize' => $pagination->pageSize,//分页大小
                'total' => $pagination->totalCount,//总条数
                'pageTotal' => ceil($pagination->totalCount / $pagination->pageSize),//分页数
                'currentPage' => ++$page//当前页
            ];
        } else {
            //否
            $list = $Model->addSelect(['`favorite_member`.*,`member`.member_name,`member`.avatar,`member`.sex,`member`.introduction'])->all();
            $returnData['list'] = $list;
            $returnData['pageArr'] = [
                'pageSize' => 0,//分页大小
                'total' => 0,//总条数
                'pageTotal' => 1,//分页数
                'currentPage' => 1//当前页
            ];
        }

        return self::result('', 0, $returnData);
    }

    /**
     * 添加
     * @param array $data
     * @return array
     */
    public function add(array $data, string $scenario = ''):array
    {
        //检查是否有被收藏的用户id
        $MemberSve = new MemberSve();
        $memberInfo = $MemberSve->getInfo(['member_id' => $data['be_favorite_member_id']]);

        if ($memberInfo['code'] === 0) {
            //检查用户是否已经收藏了
            $FMInfo = parent::getInfo(['member_id' => $data['member_id'], 'be_favorite_member_id' => $data['be_favorite_member_id']]);
            if ($FMInfo['code'] === 0) {
                return self::result('已收藏', -1, []);
            } else {
                $data['create_time'] = $_SERVER['REQUEST_TIME'];
                return parent::add($data);
            }
        } else {
            return self::result('收藏用户不存在', -1, []);
        }

    }

    /**
     * 编辑
     * @param array $data
     * @return array
     */
    public function update(array $data, string $scenario = ''):array
    {
        return parent::update($data);
    }

    /**
     * 物理删除
     * @param array $data
     * @return array
     */
    public function remove($data):array
    {
        return parent::remove($data);
    }


    /**
     * 统计用户收藏糕手个数 BY:lihouse
     * @param $memberId
     * @return int
     */
    public function addUp($memberId):int
    {
        $rs = $this->getList(['member.member_id' => $memberId]);
        if ($rs['code'] === 0) {
            return count($rs['data']['list']);
        } else {
            return 0;
        }
    }
}